<?php
/**  class.viewer.php
 * 
 * Extend Page factory in order to create legitimate pages for the blog
 * 
 * @author Ted, tass2001-at-gmail-dot-com
 * @package phpsimpleblog
 * @version $Id: class.viewer.php 13 2013-03-31 02:20:53Z tass2001@gmail.com $
 */
class Viewer extends PageFactory{
    //Construct our parent and the base Object
    public function __construct(){
        parent::__construct();
        $this->wiki_parser = new TextParser();
        $this->show_hidden = (AUTH) ? " (hidden='0' OR hidden='1')" : "hidden='0'";
    }
    
    private function __cacheData($page_num = 0){
        $return_data = array();
        $query = $this->db->query(sprintf("SELECT e.id, e.title,e.body,DATE_FORMAT(e.date,'%%b %%d, %%Y %%H:%%i') as date,e.hidden,u.name as user,c.body as comment_body,
                    c.name as comment_name, c.date as comment_date,c.id as comment_id
                    FROM %s as e
                    LEFT OUTER JOIN (SELECT id,entry_id,date,body,name FROM %s WHERE approved='1') as c ON e.id=c.entry_id
                    JOIN %s as u on e.user_id = u.id
                    WHERE %s AND special='0' ORDER BY e.date DESC LIMIT %s,10",
                    ENTRY_TABLE,COMMENT_TABLE,USERS_TABLE,
                    $this->show_hidden,$page_num*10));
        while($entry = $query->fetch_assoc()){
            if(!isset($return_data[$entry['id']]))
                $return_data[$entry['id']] = array('post' => array(),'comments'=>array());
            $comments = array();
            foreach($entry as $key=>$val){
                $return_data[$entry['id']]['post'][$key] = '';
                $val = (strpos($key,'body') !== false) ? $this->wiki_parser->parse($val) : $val; //< WikiParse any 'body' items
                if($val){
                    if(strpos($key,'comment_') !== false)
                        $comments[$key] = $val;
                    else
                        $return_data[$entry['id']]['post'][$key] = $val;
                }
            }
            if(count($comments) > 0)
                $return_data[$entry['id']]['comments'][] = $comments;
        }
        return $return_data;
    }
    
    private function __insertComments($comment_data){
        $this->appendNode($this->importFromText("<div id='comments'></div>"));
        foreach($comment_data as $comment)
            $this->appendNode($this->importFromTemplate('comment','layout',$replace_array=$comment),'comments');
    }
    
    private function __insertPosts($posts){
        foreach($posts as $post){
            $post_options = (AUTH) ? sprintf("<div class='post-edit'><a href=/post/edit/%s>Edit Post</a>/<a href='/post/delete/%s'>Delete Post</a>/<a href='/post/hide/%s'>%s</a></div>",$post['post']['id'],$post['post']['id'],$post['post']['id'],($post['post']['hidden']) ? 'Mark Public' : 'Mark Hidden') : false;
            $post_data = array_merge($post['post'],array('COMMENT_COUNT'=>count($post['comments']),'ADMIN_OPTS' => $post_options));
            $this->appendNode($this->importFromTemplate('post','layout',$post_data));
        }
    }
    
    public function viewPost($id){
        $this->blog_data = $this->__cacheData(0);
        $post_data = $this->blog_data[$id];
        if($post_data){
            $this->__insertPosts(array($post_data));
            $this->appendNode($this->importFromTemplate('comment_new','form',$replace_array=array('POST_ID'=>$post_data['post']['id'])));
            $this->__insertComments($post_data['comments']);
        }else{
            return $this->viewNotFound();
        }
        return $this->printPage();
    }
    
    public function viewHome($page){
        $this->blog_data = $this->__cacheData($page);
        $this->__insertPosts($this->blog_data,true);
        return $this->printPage();
    }
    
    public function viewLogin(){
        $this->appendNode($this->importFromTemplate('login','form'));
        return $this->printPage();
    }
    
    public function viewNotFound(){
        header('HTTP/1.0 404 Not Found');
        $this->appendNode($this->importFromTemplate('not_found','layout'));
        return $this->printPage();
    }
    
    public function viewSpecial($page_name){
        if($page_name == 'archives'){
            $archive_data = json_decode(file_get_contents(DOC_ROOT.$this->config->static_content->archives));
            foreach($archive_data as $item)
                $this->appendNode($this->importFromTemplate('archive','layout',$replace_array=array('DATE' =>$item->date,'TITLE'=>$item->title,'ID'=> $item->id)));
        }else{
            $query = $this->db->query(sprintf("SELECT ID,Body as BODY FROM %s WHERE title='%s' AND special='1'",ENTRY_TABLE,$this->esc($page_name)));
            $data = $query->fetch_assoc();
            $post_options = (AUTH) ? sprintf("<div class='post-edit'><a href=/special/edit/%s>Edit</a>/<a href='/special/delete/%s'>Delete</a></div>",$data['ID'],$data['ID']) : false; 
            if($data)
                $this->appendNode($this->importFromTemplate('special','layout',$replace_array=array('ADMIN_OPTS'=>$post_options,'BODY' => $this->wiki_parser->parse($data['BODY']),'Title' =>$data['TITLE'])));
            else
                return $this->viewNotFound();    
        }
        return $this->printPage();
    }
}